// pages/userInfo/index.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userDetails: '',
    iStatus: false,
    isFocus: false,
    editCon: '',
    oldCon: '',
    show:false,
    genderlist:['男','女']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    util.http({
      url: 'api/miniwechat/center/personal',
      data: {
        user_id: util.isLogin().id
      }
    }).then(res => {
      console.log(res)
      this.setData({
        userDetails: res
      })
    }).catch(ex => {
      wx.showToast({
        title: String(ex),
        icon: 'none',
        duration: 2000
      })
    })
  },
  
  onInput() {
    this.data.oldCon = this.data.userDetails.nickname
    this.setData({
      iStatus: true,
      isFocus: true,
      editCon: this.data.userDetails.nickname,
    })
  },
  onClose(){
    this.setData({
      show:!this.data.show
    })
  },
  onConfirm(e){
    var _that = this;
    var sex = e.detail.index + 1;
    _that.onClose();
    let data = {
      user_id: util.isLogin().user_id,
      gender: sex
    }
    uploadUserInfo(data, function (data) {
      _that.onShow();
    })
  
  },
  onChange(e) {
    this.data.editCon = e.detail
  }, 

  onBlur() {
    if (this.data.editCon === '' || this.data.editCon === null) {
      wx.showToast({
        title: '输入为空',
        icon: 'none',
        duration: 2000
      })
      this.setData({
        iStatus: false,
        isFocus: false
      })
      return false;
    } else if (this.data.editCon === this.data.oldCon) {
      wx.showToast({
        title: '修改成功',
        icon: 'none',
        duration: 2000
      })
      this.setData({
        iStatus: false,
        isFocus: false
      })
      return false;
    } else {
      let that = this;
      let data = {
        user_id: util.isLogin().user_id,
        nickname: this.data.editCon
      }
      uploadUserInfo(data, function(){
        that.onShow()
      })
    }
    this.setData({ iStatus: false });
  },

  imgChange() {
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        util.postImg({
          url: 'api/miniwechat/index/upload',
          file: tempFilePaths[0]
        }).then((imgData)=> {
          let imgInfo = JSON.parse(imgData)
          let data = {
            user_id: util.isLogin().user_id,
            avatar: imgInfo.data.url
          }
          uploadUserInfo(data, function (data) {
            that.onShow()
          })
        }).catch((err) => {
          wx.showToast({
            title: String(err),
            icon: 'none',
            duration: 2000
          })
        })
      }
    })
  },

  changeOn(e) {
    let that = this
    let sex = e.currentTarget.dataset.sex == "men" ? 1 : 2;
    let data = {
      user_id: util.isLogin().user_id,
      gender: sex
    }
    uploadUserInfo(data, function (data) {
      that.onShow();
    })
  },

  toBindPhone () {
    wx.navigateTo({
      url: '/pages/bindPhone/index'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  toBind(e) {
    wx.navigateTo({
      url: '/pages/drawInfo/index',
    })
  }
})

function uploadUserInfo(data, callback) {
  util.http({
    url: 'api/miniwechat/center/update_personal',
    isLoading: true,
    method: 'POST',
    data: data
  }).then((data) => {
    wx.showToast({
      title: '修改成功',
      icon: 'none',
      duration: 2000
    })
    callback && callback(data);
  }).catch((err) => {
    wx.showToast({
      title: err,
      icon: 'none',
      duration: 2000
    })
  })
}